<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form name="search-option__box" :inline="true">
      <el-form-item>
        <el-button type="primary" @click="addMenu">新增菜单</el-button>
        <el-button icon="el-icon-refresh" @click="fetchData" />
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      row-key="id"
      :data="menuList"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :header-cell-style="{ background: '#fafafa' }"
      default-expand-all
      border
    >
      <el-table-column prop="title" label="菜单名称" />
      <el-table-column prop="menu_type" label="菜单类型" width="100" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.menu_type==='0'">目录</span>
          <span v-else-if="scope.row.menu_type==='1'">菜单</span>
          <span v-else-if="scope.row.menu_type==='2'">按钮</span>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标" width="100" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column prop="component" label="组件" />
      <el-table-column prop="path" label="路径" />
      <el-table-column prop="perms" label="权限标识" />
      <el-table-column prop="order" label="排序" width="100" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="editMenu(scope.row)">编辑</el-button>
          <el-button type="text" @click="singleDeleteMenu(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer :title="menuDrawerTitle" :visible.sync="menuDrawerShow" direction="rtl" :before-close="handleCloseDrawer">
      <div class="drawer-container-scroll">
        <el-form ref="menuForm" :model="menuForm" :rules="menuFormRules" label-width="90px">
          <el-form-item label="菜单类型" prop="menu_type">
            <el-radio-group v-model="menuForm.menu_type">
              <el-radio-button :label="'0'">目录</el-radio-button>
              <el-radio-button :label="'1'">菜单</el-radio-button>
              <el-radio-button :label="'2'">按钮</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="上级菜单" prop="parent_id">
            <div class="select-container">
              <SelectTree
                ref="treeSelect"
                v-model="menuForm.parent_id"
                :data="menuOptions"
              />
            </div>
          </el-form-item>
          <el-form-item label="菜单名称" prop="title">
            <el-input v-model="menuForm.title" placeholder="请输入菜单名称" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="路由地址" prop="path">
            <el-input v-model="menuForm.path" placeholder="请输入路由地址" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="组件路径" prop="component">
            <el-input v-model="menuForm.component" placeholder="请输入组件路径, 如: dashboard/index" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="重定向路径" prop="redirect">
            <el-input v-model="menuForm.redirect" placeholder="请输入重定向路径" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="菜单图标" prop="icon">
            <el-input v-model="menuForm.icon" placeholder="请输入菜单图标" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="高亮菜单" prop="icon">
            <el-input v-model="menuForm.activeMenu" placeholder="请输入高亮菜单" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type === '2'" label="权限标识">
            <el-input v-model="menuForm.perms" placeholder="请输入权限标识" />
          </el-form-item>
          <el-form-item label="排序" prop="order">
            <el-input-number v-model="menuForm.order" :min="1" :max="999" label="排序序号" />
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="状态">
            <el-radio-group v-model="menuForm.disabled">
              <el-radio :label="false">正常</el-radio>
              <el-radio :label="true">停用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type !== '2'" label="显示状态" prop="hidden">
            <el-radio-group v-model="menuForm.hidden">
              <el-radio :label="false">显示</el-radio>
              <el-radio :label="true">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="menuForm.menu_type === '1'" label="页面缓存" prop="noCache">
            <el-radio-group v-model="menuForm.noCache">
              <el-radio :label="false">缓存</el-radio>
              <el-radio :label="true">不缓存</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="drawer-footer__btn">
          <el-button @click="handleCloseDrawer">取 消</el-button>
          <el-button type="primary" @click="saveSubmit">保 存</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import apiMenu from '@/api/system/menu'
import SelectTree from '@/components/SelectTree'
const defaultForm = {
  title: '',
  menu_type: '0',
  parent_id: null,
  disabled: false,
  hidden: false,
  noCache: false,
  path: '',
  component: '',
  activeMenu: '',
  icon: '',
  order: 1,
  perms: ''
}

export default {
  components: { SelectTree },
  data() {
    return {
      menuList: [],
      menuOptions: [],
      listLoading: false,
      menuDrawerTitle: '新增菜单',
      menuDrawerShow: false,
      menuForm: defaultForm,
      menuFormRules: {
        title: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '路由地址不能为空', trigger: 'blur' }
        ],
        order: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      apiMenu.getMenuList().then(response => {
        this.menuList = response.data
        this.total = response.count
      })
      this.listLoading = false
    },
    getMenuOptions() {
      apiMenu.getMenuOptions().then(response => {
        this.menuOptions = response.data
      })
    },
    addMenu() {
      this.getMenuOptions()
      this.menuDrawerShow = true
      this.menuDrawerTitle = '新增菜单'
      this.submitType = 0
      this.menuForm = Object.assign({}, defaultForm)
    },
    editMenu(row) {
      this.getMenuOptions()
      this.menuDrawerShow = true
      this.menuDrawerTitle = '编辑菜单'
      this.submitType = 1
      this.menuForm = Object.assign({}, row)
    },
    async saveSubmit() {
      await this.$refs.menuForm.validate(async validate => {
        if (validate) {
          if (this.submitType === 0) {
            await apiMenu.createMenu(this.menuForm).then(response => {
              this.$message.success(response.message || '新增成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          } else {
            await apiMenu.updateMenu(this.menuForm).then(response => {
              this.$message.success(response.message || '编辑成功')
            }).catch(error => {
              this.$message.error(error.response.data['message'])
            })
          }
          this.menuDrawerShow = false
          await this.fetchData()
        }
      })
    },
    async singleDeleteMenu(row) {
      const data = [row.id]
      await this.deleteSubmit(data)
    },
    async deleteSubmit(data) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        lockScroll: false,
        type: 'warning'
      }).then(async() => {
        await apiMenu.deleteMenu(data).then(async response => {
          this.$message.success(response.message || '删除成功')
          await this.fetchData()
        })
      })
    },
    handleCloseDrawer() {
      this.menuDrawerShow = false
    }
  }
}
</script>
